<template>
  <div>
    <!-- 导航栏 -->
    <nav-bar />
    <!-- 表单域 -->
    <van-form @submit="register">
      <van-field
        v-model="regList.phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, validator, message: '请输入正确的手机号' }]"
      />
      <van-field
        v-model="regList.nickname"
        name="昵称"
        label="昵称"
        placeholder="请输入昵称"
        :rules="[{ required: true, message: '请输入昵称' }]"
      />
      <van-field
        v-model="regList.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" submit-on-enter
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import navBar from "@/components/navBar";
import { registerInfo } from "@/util/axios";
// 轻提示
import { Toast } from "vant";
export default {
  data() {
    return {
      regList: {
        phone: "", //手机号
        nickname: "", //昵称
        password: "", //密码
      },
    };
  },
  components: {
    navBar,
  },
  methods: {
    //校验函数返回 true 表示校验通过，false 表示不通过
    validator(val) {
      return /1\d{10}/.test(val);
    },
    //注册
    register() {
      registerInfo(this.regList).then((res) => {
        if (res.code == 200) {
          Toast.success(res.msg);
          this.$router.push("/login");
        } else {
          Toast.fail(res.msg);
        }
      });
    },
  },
};
</script>

<style lang="" scoped>
</style>